package com.gnrsoft.springboot_test.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;

/**
 * 执行时间切面
 *
 */
@Aspect
@Configuration
@Slf4j
public class TimeAspect {

	/*
	 * 执行时间切点 可传参注解 ，
	 */
	@Pointcut("@annotation(execTime)")
	public void execTimePointcut(ExecTime execTime) {

	}

	/*
	 * 环绕通知 打印业务方法 执行时间， 通知调 切面表达式
	 */
	@Around(value = "execTimePointcut(execTime)")
	public Object getCache(ProceedingJoinPoint joinPoint, ExecTime execTime) {
		
		long startTime = System.currentTimeMillis();
		//代理对象
//		String thisName = joinPoint.getThis().getClass().getName();
		//目标对象
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		
		log.info("className " + targetName + " methodName " + methodName + " start");
		Object result = null;

		try {
			result = joinPoint.proceed(joinPoint.getArgs());
		} catch (Throwable e) {
			log.error("className " + targetName + " methodName " + methodName + " exec error ", e);
		}
		
		log.info("className " + targetName + " methodName " + methodName + " end execTime " + (System.currentTimeMillis() - startTime )
				+ " ms");
		
		return result;
	}

}
